package com.bei_en.teacher.admin.controller.regions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.regions.bean.Regions;
import com.bei_en.teacher.admin.model.regions.condition.RegionsCondition;
import com.bei_en.teacher.admin.model.regions.vo.RegionsVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.regions.RegionsService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;

/**
 * 
 * @author Guocg
 * @date 2016-10-3 23:27:08
 */
@Controller
@RequestMapping("/api/Regions")
public class RegionsAPPController extends BaseController {

	@Autowired
	RegionsService regionsService;

	@RequestMapping("/regions")
	@ResponseBody
	public JsonResult regions(Model model, @Valid RegionsCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {

			List<RegionsVo> list = new ArrayList<RegionsVo>();
			list = regionsService.findRegionsPage(vo);
			JSONArray array = new JSONArray();
			JSONObject object = null;
			RegionsVo regionsVo = null;
			if (vo.getProvinceId() != null) {
				switch (vo.getProvinceId()) {
				case 0:
					for (int i = 0; i < list.size(); i++) {
					
						object = new JSONObject();
						regionsVo = list.get(i);
						if(regionsVo.getItemId()==3266){
							continue;
						}
					switch (regionsVo.getItemId()) {
					case 33:
					case 34:
					case 35:
					case 36:
					case 37:
					case 45:
					case 51:
						seetingJsonObject(object, "itemId",
								regionsVo.getItemId());
						seetingJsonObject(object, "cityId",
								regionsVo.getCityId());
						seetingJsonObject(object, "realId",	regionsVo.getItemId()
								);
						break;

					default:
						seetingJsonObject(object, "itemId",
								"3266"	);
						seetingJsonObject(object, "realId",	regionsVo.getItemId()
								);
						break;
					}
						seetingJsonObject(object, "provinceId",
								regionsVo.getProvinceId());
				
						seetingJsonObject(object, "areaId",
								regionsVo.getAreaId());
						seetingJsonObject(object, "name",
								regionsVo.getItemName());
						array.add(object);
					}
					break;
				default:
					object = new JSONObject();
					regionsVo = list.get(0);
					seetingJsonObject(object, "itemId", regionsVo.getItemId());
					seetingJsonObject(object, "provinceId",
							regionsVo.getProvinceId());
					seetingJsonObject(object, "realId",	regionsVo.getItemId()
							);
					seetingJsonObject(object, "cityId", regionsVo.getCityId());
					seetingJsonObject(object, "areaId", regionsVo.getAreaId());
					seetingJsonObject(object, "name", "统考");
					array.add(object);
					break;
				}
			} else {
				for (int i = 0; i < list.size(); i++) {
					object = new JSONObject();
					regionsVo = list.get(i);
					seetingJsonObject(object, "itemId", regionsVo.getItemId());
					seetingJsonObject(object, "provinceId",
							regionsVo.getProvinceId());
					seetingJsonObject(object, "cityId", regionsVo.getCityId());
					seetingJsonObject(object, "realId",	regionsVo.getItemId()
							);
					seetingJsonObject(object, "realId",	regionsVo.getItemId()
							);
					seetingJsonObject(object, "areaId", regionsVo.getAreaId());
					seetingJsonObject(object, "name", regionsVo.getItemName());
					array.add(object);
				}
			}

			return new JsonSuccessResult(session.getId(), array);

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/regionsAdd")
	@ResponseBody
	public JsonResult regionsAdd(Model model, @Valid RegionsCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
		try {

			RegionsVo regions = new RegionsVo();

			regions.setItemId(vo.getItemId());
			regions.setProvinceId(vo.getProvinceId());
			regions.setCityId(vo.getCityId());
			regions.setAreaId(vo.getAreaId());
			regions.setItemName(vo.getItemName());
			regions.setOrderBy(vo.getOrderBy());
			regions.setAreaCode(vo.getAreaCode());
			regions.setIsOpened(vo.getIsOpened());

			regionsService.insert(regions);
			return new JsonSuccessResult(session.getId(), "成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

	@RequestMapping("/regionsEdit")
	public JsonResult regionsEdit(Model model, @Valid RegionsCondition vo,
			BindingResult br, HttpSession session) {
		if (br.hasErrors())
			return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());

		try {
			RegionsVo regions = new RegionsVo();

			regionsService.updateSelective(regions);

			return new JsonSuccessResult(session.getId(), "成功");

		} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
	}

}
